﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;
using RelationTextBox;
using System.Diagnostics;
using System.Threading;
using System.Globalization;
using System.Reflection;

namespace ExBug
{
    public partial class CheltuieliFrm : FrmRezumat
    {

        TreeNode tnRoot = new TreeNode();
        private Int16 AnCurent = 0;
        private Int16 LunaCurenta = 0;
        private Boolean init_form = true;
        private Boolean modificat = false;
        private Boolean modificatSP = false;
        private Boolean ColoaneDepartamente = true;

        public CheltuieliFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            diviziuneMonedaSursaBindingSource.List.Add(new DiviziuneMonedaSursa("Lei", 0));
            diviziuneMonedaSursaBindingSource.List.Add(new DiviziuneMonedaSursa("Mii lei", 1));
            numeMacheta = Text;
            eSRPDataSetExecutie.ExecutieCheltuieli.ExecutieCheltuieliRowChanged += ExecutieCheltuieli_ExecutieCheltuieliRowChanged;
            eSRPDataSetExecutie.ExecutieCheltuieliPacienti.ExecutieCheltuieliPacientiRowChanged += ExecutieCheltuieliPacienti_ExecutieCheltuieliPacientiRowChanged;
        }

        void ExecutieCheltuieliPacienti_ExecutieCheltuieliPacientiRowChanged(object sender, ESRPDataSetExecutie.ExecutieCheltuieliPacientiRowChangeEvent e)
        {
            if (e.Action == DataRowAction.Change)
            {
                modificatSP = true;
            }

        }

        void ExecutieCheltuieli_ExecutieCheltuieliRowChanged(object sender, ESRPDataSetExecutie.ExecutieCheltuieliRowChangeEvent e)
        {
            if (e.Action == DataRowAction.Change)
            {
                modificat = true;
            }
        }

        private void BugetFrm_Load(object sender, EventArgs e)
        {
          
            SqlConnection con=DeschideConexiune(workConnString);
            LoadSetup(con);
            this.caseAsigurariSanatateTableAdapter.Connection = con;
            this.caseAsigurariSanatateTableAdapter.Fill(this.eSRPDataSetExecutie.CaseAsigurariSanatate);

            this.executieCheltuieliPacientiTableAdapter.Connection = con;
            this.executieCheltuieliPacientiTableAdapter.FillByDepartamente(this.eSRPDataSetExecutie.ExecutieCheltuieliPacienti,ModuleInfo.CodUnitate);


            this.departamenteTableAdapter.Connection = con;
            if (ColoaneDepartamente)
            {
                this.departamenteTableAdapter.Fill(this.eSRPDataSetExecutie.Departamente, ModuleInfo.CodUnitate);
            }
            else
                this.departamenteTableAdapter.FillByExecutie(this.eSRPDataSetExecutie.Departamente, ModuleInfo.CodUnitate);

            this.jurnaleTableAdapter.Connection = con;
            this.jurnaleTableAdapter.Fill(eSRPDataSetExecutie.Jurnale, ModuleInfo.CodUnitate);
            this.executieCheltuieliTableAdapter.Connection = con;
            this.executieCheltuieliTableAdapter.FillByDepartamente(eSRPDataSetExecutie.ExecutieCheltuieli, ModuleInfo.CodUnitate);
            articoleAliniateTableAdapter.Connection = con;
            articoleAliniateTableAdapter.FillByExecutieCheltuieli(eSRPDataSetExecutie.ArticoleAliniate, ModuleInfo.CodUnitate);
            this.exbug_GenereazaExecutieCheltuieliTotalTableAdapter.Connection = con;
            this.exbug_GenereazaExecutieCheltuieliTotalTableAdapter.Fill(this.eSRPDataSetExecutie.exbug_GenereazaExecutieCheltuieliTotal,
                ModuleInfo.CodUnitate, lunaDTP.Value, ArticolAliniatCB.SelectedValue.ToString(), (int?)JurnalCB.SelectedValue);

            InchideConexiune(con);
            Text = numeMacheta ;
            init_form = false;
            //AddButton("Genereaza NC", global::ERPStudio.Properties.Resources.salvare1, GenereazaNC, ToolStripItemAlignment.Right, true);
            //AddButton("Setaza data", global::ERPStudio.Properties.Resources.info, Initializare, ToolStripItemAlignment.Right, false);
            DiviziuneMonetaraCB.SelectedIndex = 0;

            if(ColoaneDepartamente)
                AddBindingSource(executieCheltuieliDepartamenteBindingSource);
            else
                AddBindingSource(executieCheltuieliBindingSource);
            AddTableAdaptor(executieCheltuieliTableAdapter.Adapter);

            AddBindingSource(fKExecutieCheltuieliPacientiCaseAsigurariSanatateBindingSource);
            AddTableAdaptor(executieCheltuieliPacientiTableAdapter.Adapter);

            executieCheltuieliBindingSource.Sort = "ArtAl";

            fKExecutieCheltuieliPacientiCaseAsigurariSanatateBindingSource.Filter = " Data='" + AnCurent.ToString() + "-" + LunaCurenta.ToString() + "-01'";
            executieCheltuieliBindingSource.Filter = " Data='" + AnCurent.ToString() + "-" + LunaCurenta.ToString() + "-01'";
            executieCheltuieliDepartamenteBindingSource.Filter = " Data='" + AnCurent.ToString() + "-" + LunaCurenta.ToString() + "-01'";
       }

        public void LoadSetup(SqlConnection sqlConn)
        {
            Object ret = GetSetupValue("ExBugSetup", "AnulCurent", ModuleInfo.CodUnitate, sqlConn);
            if (ret == null)
            { 
                //adauga o inregistrare in exbug pentru ca unitatea nu e configurata
                SqlCommand cmd = new SqlCommand("INSERT INTO [ExBugSetup]([CompartimentSpecialitate],[OrdonatorCredite],[CodUnitate],[AnCurent])"+
                "VALUES('',''," + ModuleInfo.CodUnitate.ToString() + "," + DateTime.Now.Year.ToString() + ")", sqlConn);
                cmd.ExecuteNonQuery();
                ret = GetSetupValue("ExBugSetup", "AnCurent", ModuleInfo.CodUnitate, sqlConn);
            }
            
            AnCurent = (Int16)ret;

            ret = GetSetupValue("ExBugSetup", "LunaCurenta", ModuleInfo.CodUnitate, sqlConn);
            LunaCurenta = (Int16)ret;

            lunaDTP.Value = new DateTime(AnCurent, LunaCurenta, 1);
            LunaSPDTP.Value = new DateTime(AnCurent, LunaCurenta, 1);
            ret = GetSetupValue("ExBugSetup", "ColoanaDepartamente", ModuleInfo.CodUnitate, sqlConn);
            ColoaneDepartamente = Convert.ToBoolean(ret);

            ArticolAliniatCB.Visible = ColoaneDepartamente;
            ArticolAliniatL.Visible = ColoaneDepartamente;
            DepartamentCB.Visible = !ColoaneDepartamente;
            DepartamentL.Visible = !ColoaneDepartamente;
            DenumireArtAlCB.Visible = ColoaneDepartamente;
            BugetDGV.Visible = !ColoaneDepartamente;
            executieCheltuieliDGV.Visible = ColoaneDepartamente;
            //exbug_GenereazaExecutieCheltuieliTotalDGV.Visible = !ColoaneDepartamente;
            
        }
        /// <summary>
        /// functia care incarca coloanele
        /// </summary>
        

        private void JurnalCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (modificat || modificatSP)
            {
                Salvare(sender, e);
                modificat = false;
                modificatSP = false;
            }

            init_form = true;
            Cursor.Current = Cursors.WaitCursor;

            if ((JurnalCB.SelectedValue != null && DepartamentCB.SelectedValue != null && !ColoaneDepartamente) ||
                (JurnalCB.SelectedValue != null && ArticolAliniatCB.SelectedValue != null && ColoaneDepartamente))
            {
                SqlConnection con = DeschideConexiune(workConnString);
                DateTime dt = new DateTime(lunaDTP.Value.Year, lunaDTP.Value.Month, 1);
                SqlCommand com = new SqlCommand("exbug_GenereazaExecutieCheltuieli", con);
                if (ColoaneDepartamente)
                    com.CommandText = "exbug_GenereazaExecutieCheltuieliArtAl";
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.Add(new SqlParameter("CodUnitate", ModuleInfo.CodUnitate));
                com.Parameters.Add(new SqlParameter("Data", dt));
                if(!ColoaneDepartamente)
                    com.Parameters.Add(new SqlParameter("IDDepartament", DepartamentCB.SelectedValue));
                else
                    com.Parameters.Add(new SqlParameter("ArtAl", ArticolAliniatCB.SelectedValue));
                com.Parameters.Add(new SqlParameter("IDJurnal", JurnalCB.SelectedValue));
                com.ExecuteNonQuery();

                this.executieCheltuieliTableAdapter.Connection = con;
                this.executieCheltuieliTableAdapter.FillByDepartamente(this.eSRPDataSetExecutie.ExecutieCheltuieli, ModuleInfo.CodUnitate);


                this.exbug_GenereazaExecutieCheltuieliTotalTableAdapter.Connection = con;
                this.exbug_GenereazaExecutieCheltuieliTotalTableAdapter.Fill(this.eSRPDataSetExecutie.exbug_GenereazaExecutieCheltuieliTotal,
                    ModuleInfo.CodUnitate, lunaDTP.Value,ArticolAliniatCB.SelectedValue.ToString(), (int)JurnalCB.SelectedValue);
                
                InchideConexiune(con);
            }

            //fKExecutieCheltuieliPacientiCaseAsigurariSanatateBindingSource.Filter = " Data='" + lunaDTP.Value.Year.ToString() + "-" + lunaDTP.Value.Month.ToString() + "-01'";
            executieCheltuieliBindingSource.Filter = " Data='" + lunaDTP.Value.Year.ToString() + "-" + lunaDTP.Value.Month.ToString() + "-01'";
            executieCheltuieliDepartamenteBindingSource.Filter = " Data='" + lunaDTP.Value.Year.ToString() + "-" + lunaDTP.Value.Month.ToString() + "-01'";
            //executieCheltuieliBindingSource.Filter = "Data='" + lunaDTP.Value.Year.ToString() + "." + lunaDTP.Value.Month.ToString() + ".01'";
            Cursor.Current = Cursors.Default;
            init_form = false;

        }

        private void GenereazaNC(object sender, EventArgs e)
        {
            FrmMainMdi frm = this.MdiParent as FrmMainMdi;
            Assembly asm = frm.GetModule("esrp");
            if (asm == null)
                return;
            String numeClasa = "ESRP.FormulaContabilaFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                //FrmCard newFrm = asm.CreateInstance(numeClasa) as FrmCard;
                FrmWorkspace newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                    new object[] { }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmWorkspace;
                mModule.InvokeMember("GenereazaNC", BindingFlags.SetProperty, null, newFrm, new object[] { (Int16)1 });

                newFrm.MdiParent = frm;
                newFrm.Show();
            }
        }


        override public void Salvare(object sender, EventArgs args)
        {
            base.Salvare(this, args);
            if (EroareSalvare)
                return;

            modificat = false;
            modificatSP = false;
            if (JurnalCB.SelectedValue != null && DepartamentCB.SelectedValue != null)
            {
                SqlConnection con = DeschideConexiune(workConnString);

                this.exbug_GenereazaExecutieCheltuieliTotalTableAdapter.Connection = con;
                this.exbug_GenereazaExecutieCheltuieliTotalTableAdapter.Fill(this.eSRPDataSetExecutie.exbug_GenereazaExecutieCheltuieliTotal,
                    ModuleInfo.CodUnitate, lunaDTP.Value, ArticolAliniatCB.SelectedValue.ToString(), (int)JurnalCB.SelectedValue);
                InchideConexiune(con);
            }
        }

        private void ArticolSPCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (modificatSP || modificat)
            {
                Salvare(sender, e);
                modificatSP = false;
                modificat = false;
            }


            init_form = true;
            Cursor.Current = Cursors.WaitCursor;

            if (ArticolSPCB.SelectedValue != null && JurnalCB.SelectedValue!=null)
            {
                DateTime dt = new DateTime(LunaSPDTP.Value.Year, LunaSPDTP.Value.Month,1);
                SqlConnection con = DeschideConexiune(workConnString);

                SqlCommand com = new SqlCommand("exbug_ExecutieCheltuieliPacientiArtAl", con);

                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.Add(new SqlParameter("CodUnitate", ModuleInfo.CodUnitate));
                com.Parameters.Add(new SqlParameter("Data", dt));
                
                com.Parameters.Add(new SqlParameter("IDCasaAsigSanatate", ArticolSPCB.SelectedValue));
                com.Parameters.Add(new SqlParameter("IDJurnal", JurnalCB.SelectedValue));
                com.ExecuteNonQuery();

                this.executieCheltuieliPacientiTableAdapter.Connection = con;
                this.executieCheltuieliPacientiTableAdapter.Fill(this.eSRPDataSetExecutie.ExecutieCheltuieliPacienti,ModuleInfo.CodUnitate);
                
                InchideConexiune(con);
            }
            //executieCheltuieliBindingSource.Filter = "Data='" + LunaSPDTP.Value.Year.ToString() + "." + LunaSPDTP.Value.Month.ToString() + ".01'";
            //executieCheltuieliDepartamenteBindingSource.Filter = " Data='" + LunaSPDTP.Value.Year.ToString() + "-" + LunaSPDTP.Value.Month.ToString() + "-01'";
            fKExecutieCheltuieliPacientiCaseAsigurariSanatateBindingSource.Filter = " Data='" + LunaSPDTP.Value.Year.ToString() + "-" + LunaSPDTP.Value.Month.ToString() + "-01'";
            Cursor.Current = Cursors.Default;
            init_form = false;
        }

        

    }
    
}
